const app = getApp();
Component({
    options: {
        multipleSlots: true // 在组件定义时的选项中启用多slot支持
    },
    data: {
        isShow: false,
        option: {
            title: '消息框',
            type: '',
            btnArray: [{
                    title: '取消',
                    name: 'cancel',
                    class: ''
                },
                {
                    title: '确定',
                    name: 'confirm',
                    class: 'confirm'
                }
            ],
            icon: 'success',
            duration: 1500,
            callback: null,
            customize: null,
            
        },
        Pagerbtn:'关闭',
        PagerLists:[
            {
                name:'不开启',
                val:'0',
                ischeck:false
            },
            {
                name:'15分钟后',
                val:'900',
                ischeck:false
            },
            {
                name:'30分钟后',
                val:'1800',
                ischeck:false
            },
            {
                name:'60分钟后',
                val:'3600',
                ischeck:false
            },
            {
                name:'120分钟后',
                val:'7200',
                ischeck:false
            },
            {
                name:'240分钟后',
                val:'14400',
                ischeck:false
            },
        ],
        closetimecurrent:0,
        closetimeend:false,
        closetimeindex:-1,
        isclosetime:false,
    },
    ready:function () {
        if(app.globalData.closetimecurrent>=0){
          this.setData({
            closetimecurrent:app.globalData.closetimecurrent,
            closetimeindex:app.globalData.closetimeindex,
            isclosetime:app.globalData.isclosetime
          })
          this.settimefun()
        }

    },
    methods: {
        //展示弹框
        ShowDialog(option) {
            //默认参数
            let defaultOption = {
                title: '消息框',
                type: '',
                btnArray: [{
                        title: '取消',
                        name: 'cancel',
                        class: ''
                    },
                    {
                        title: '确定',
                        name: 'confirm',
                        class: 'confirm'
                    }
                ],
                icon: 'success',
                duration: 1500,
                callback: null,
                customize: null,
                
            }
            //合并参数
            let _option = Object.assign(defaultOption, option);
            this.setData({
                option: _option,
                isShow: true
            })
            //定时关闭
            setTimeout(() => {
                this.DialogTimeOut()
            }, 0);
        },
        CloseDialog() {
            if (this.data.isShow) {
                //关闭弹窗触发toastClose事件
                this.triggerEvent("toastClose");
            }
            this.setData({
                isShow: false
            })
        },
        DialogTimeOut() {
            //定时消失
            if (this.data.isShow && (this.data.option.type === "Message" || this.data.option.type === "Slot") && this.data.option.duration > 0) {
                setTimeout(() => {
                    this.setData({
                        isShow: false
                    });
                    //触发toastClose事件
                    this.triggerEvent("toastClose");
                }, this.properties.option.duration)
            }
        },
        _tapEvent(event) {
            //调用回调
            if (this.data.option.callback && typeof this.data.option.callback == 'function') {
                this.data.option.callback({
                    index: event.target.dataset.index,
                    name: event.target.dataset.name,
                    customize: this.data.customize,
                });
            }
            this.triggerEvent("toastEvent", {
                index: event.target.dataset.index,
                name: event.target.dataset.name,
                customize: this.data.customize
            });
            
        },
        selectpacker(e) {
            var index=e.currentTarget.dataset.index;
            var time=Number(e.currentTarget.dataset.time);
            var that = this;
            if(index==that.data.closetimeindex){
                return
            }
            clearInterval(app.globalData.setInter)
            app.globalData.setInter=null
            that.setData({
                closetimeindex:index,
                closetimecurrent:time,
            })
            app.globalData.closetimeindex=index
            app.globalData.closetimecurrent=time
            if(time<=0){
                clearInterval(app.globalData.setInter)
                that.triggerEvent("toasttime", {
                    close:true
                })
            }else{
                that.setData({
                    isclosetime:true
                })
                that.settimefun()
            }
        },
        settimefun(){
            clearInterval(app.globalData.setInter)
            var that = this;
            var time = app.globalData.closetimecurrent;
            if(!this.data.isclosetime){
                return
            }
            app.globalData.setInter = setInterval(res => {
                
                time--;
                that.setData({
                    closetimecurrent:time,
                    closetimeindex:app.globalData.closetimeindex
                })
                app.globalData.closetimecurrent=time
                if (time <= 0) {
                    clearInterval(app.globalData.setInter)
                    app.globalData.setInter=null
                    app.globalData.closetimecurrent=0
                    app.globalData.play=1
                }
                that.triggerEvent("toasttime", {
                    time:app.globalData.closetimecurrent
                });
            }, 1000)
        },
        clearsettimefun(){
            clearInterval(app.globalData.setInter)
        }
    }
})